package top.tangyh.lamp.care.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.gson.Gson;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import top.tangyh.basic.base.R;
import top.tangyh.basic.exception.BizException;
import top.tangyh.basic.interfaces.echo.EchoService;
import top.tangyh.basic.base.controller.SuperController;
import top.tangyh.lamp.care.entity.BDevBedRelation;
import top.tangyh.lamp.care.mqtt.send.SendMqtt;
import top.tangyh.lamp.care.mqtt.send.dto.Msg;
import top.tangyh.lamp.care.properties.MqttProperties;
import top.tangyh.lamp.care.service.BDevBedRelationService;
import top.tangyh.lamp.care.service.BDevBindService;
import top.tangyh.lamp.care.entity.BDevBind;
import top.tangyh.lamp.care.service.BPatientService;
import top.tangyh.lamp.care.vo.query.BindInfoQueryVO;
import top.tangyh.lamp.care.vo.query.PatientBindInputVO;
import top.tangyh.lamp.care.vo.result.*;
import top.tangyh.lamp.care.vo.save.BDevBindSaveVO;
import top.tangyh.lamp.care.vo.save.BindInVO;
import top.tangyh.lamp.care.vo.save.UnBindInVO;
import top.tangyh.lamp.care.vo.update.BDevBindUpdateVO;
import top.tangyh.lamp.care.vo.query.BDevBindPageQuery;
import top.tangyh.lamp.common.utils.IcuDateUtils;

import javax.validation.Valid;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * 监护仪设备绑定中间表
 * </p>
 *
 * @author zqb
 * @date 2024-07-15 16:42:41
 * @create [2024-07-15 16:42:41] [zqb] [代码生成器生成]
 */
@Slf4j
@RequiredArgsConstructor
@Validated
@RestController
@RequestMapping("/bDevBind")
@Tag(name = "监护仪设备绑定中间")
public class BDevBindController {
    //    extends SuperController<BDevBindService, Long, BDevBind
//            , BDevBindSaveVO, BDevBindUpdateVO, BDevBindPageQuery, BDevBindResultVO>
//    private final EchoService echoService;
//    @Override
//    public EchoService getEchoService() {
//        return echoService;
//    }
    private final BPatientService bPatientService;

    private final BDevBindService bindService;

    private final BDevBedRelationService bDevBedRelationService;

    private final MqttProperties mqttProperties;

    @Operation(summary = "绑定")
    @PostMapping
    public R bind(@RequestBody @Valid BindInVO bindInVO) {

//        TestWebservice testWebservice = new TestWebservice();
//        //webservice数据
//        List<FuerInpatientInfoResult> list;
//        list = testWebservice.analysisData();
//        FuerInpatientInfoResult fuerInpatientInfoResult=bPatientService.getPatientInfo(bindInVO.getHospitalNumber(),list);

        bindService.bind(bindInVO);

        Msg msg =new Msg();
        msg.setRefresh(1);
        msg.setType(1);
        msg.setSendTime(IcuDateUtils.getGivenZoneTimeStr(LocalDateTime.now()));

        Gson gson = new Gson();
        String content = gson.toJson(msg);

        SendMqtt.sendToMQTT(content,mqttProperties.getDefaultTopic(),mqttProperties.getHostUrl(),
                mqttProperties.getClientId(),mqttProperties.getUsername(),mqttProperties.getPassword());

        return R.success();
    }


    @Operation(summary = "设为常用")
    @PostMapping("/set/common")
    public R setCommon(@RequestBody @Valid BDevBedRelation devBedRelation) {
        int count = bDevBedRelationService.count(devBedRelation);
        if (count > 0) {
            throw new BizException("设置超过数量限制,请先取消原有常用关系");
        }
        bDevBedRelationService.save(devBedRelation);
        return R.success();
    }

    @Operation(summary = "取消常用")
    @DeleteMapping("/del/common")
    public R delCommon(String devCode, Integer devType) {
        return R.success(bDevBedRelationService.deleteByDevCode(devCode, devType));
    }


    @Operation(summary = "解绑")
    @PutMapping
    public R unBind(@RequestBody @Valid UnBindInVO unBindInVO) {
//        TestWebservice testWebservice = new TestWebservice();
//        //webservice数据
//        List<FuerInpatientInfoResult> list;
//        list = testWebservice.analysisData();
//        FuerInpatientInfoResult fuerInpatientInfoResult=bPatientService.getPatientInfo(unBindInVO.getHospitalNumber(),list);

        bindService.unBind(unBindInVO);

        Msg msg =new Msg();
        msg.setRefresh(1);
        msg.setType(1);
        msg.setSendTime(IcuDateUtils.getGivenZoneTimeStr(LocalDateTime.now()));

        Gson gson = new Gson();
        String content = gson.toJson(msg);

        SendMqtt.sendToMQTT(content,mqttProperties.getDefaultTopic(),mqttProperties.getHostUrl(),
                mqttProperties.getClientId(),mqttProperties.getUsername(),mqttProperties.getPassword());

        return R.success();
    }

    @Operation(summary = "查询已绑定数据")
    @GetMapping
    public R<PageOutputDTO<BindInfoOutputVO>> queryBindList(BindInfoQueryVO queryInputDTO) {
        PageOutputDTO<BindInfoOutputVO> pageList = bindService.selectPageListByQuery(queryInputDTO);
        return R.success(pageList);
    }

    @Operation(summary = "查询该病人的绑定设备")
    @GetMapping("/patientBind")
    public R<List<PatientBindOutputVO>> queryPatientBind(@Valid PatientBindInputVO patientBindInputVO) {
        List<PatientBindOutputVO> patientBindOutputVO = bindService.queryPatientBindFuer(patientBindInputVO);
        return R.success(patientBindOutputVO);
    }


}


